<?php
class Default_Service_ShipType
{

    public static function add($row){
        $shipTypeObj = new Default_Model_ShipType();
        return $shipTypeObj->add($row);
	}

	public static function update($row,$value,$field='st_id'){
        $shipTypeObj = new Default_Model_ShipType();
        return $shipTypeObj->update($row,$value,$field);
	}


	public static function delete($value,$field='st_id'){
        $shipTypeObj = new Default_Model_ShipType();
        return $shipTypeObj->delete($value,$field);

	}

	public static function getByValue($value,$field='st_id'){
        $shipTypeObj = new Default_Model_ShipType();
        return $shipTypeObj->getByValue($value,$field);
	}

    public static function getByCondition($condition,$type='*',$orderby="",$page=0,$pageSize=0){
        $shipTypeObj = new Default_Model_ShipType();
        return $shipTypeObj->getByCondition($condition,$type,$orderby,$page,$pageSize);
	}

    public static function getStClassCode() {
        return array(
            'CRE' =>'快递',
            'CRP' =>'挂号',
            'CPS' =>'平邮',
        );
    }

    public static function getStatusList() {
        return array(
            '0' =>'不可用',
            '1' =>'可用',
        );
    }

    public static function getInfo($shipTypeList) {
        $stClassCode = self::getStClassCode();
        $statusList = self::getStatusList();
        foreach($shipTypeList as  $key =>$row){
            $shipTypeList[$key]['st_class_name'] = @$stClassCode[$row['st_class_code']];
            $shipTypeList[$key]['st_status_name'] = @$statusList[$row['st_status']];
        }
        return $shipTypeList;
    }


    public static function validator($val) {
        $error = array();
        $validateArr = array();
        $validateArr[] = array('name' => '运输方式代号', 'value' => $val['st_code'], 'regex' => array('require', 'length[0,255]', 'noCharacter'));
        $validateArr[] = array('name' => '运输方式中文名', 'value' => $val['st_name_cn'], 'regex' => array('require','length[0,255]'));
        $validateArr[] = array('name' => '处理费', 'value' => $val['st_deal_fee'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '挂号费', 'value' => $val['st_reg_fee'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '燃油附加费', 'value' => $val['st_addons'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '运费折扣', 'value' => $val['st_discount'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '货运最佳时效', 'value' => $val['st_delivery_time_min'], 'regex' => array('positive1'));
        $validateArr[] = array('name' => '货运最低时效', 'value' => $val['st_delivery_time_max'], 'regex' => array('positive1'));
        $validateArr[] = array('name' => '体积重量转换率', 'value' => $val['st_vol_weight_rate'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '是否体积重计算', 'value' => $val['st_is_volume'], 'regex' => array('require'));
        $validateArr[] = array('name' => '状态', 'value' => $val['st_status'], 'regex' => array('require'));
        $validateArr[] = array('name' => '运输方式类型', 'value' => $val['st_class_code'], 'regex' => array('require'));
        $validateArr[] = array('name' => '仓库', 'value' => $val['warehouse_id'], 'regex' => array('require'));
        $validateArr[] = array('name' => '最低折扣', 'value' => $val['st_discount_min'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '最低处理费', 'value' => $val['st_deal_fee_min'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '最低挂号费', 'value' => $val['st_reg_fee_min'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '排序序号', 'value' => $val['st_sort'], 'regex' => array('positive'));
        $validateArr[] = array('name' => '体积重限制', 'value' => $val['st_limit_volume'], 'regex' => array('positive'));
        $error = Common_Service_Validator::formValidator($validateArr);

        if($val['st_id']>0 && !empty($val['st_code'])){
            $condition = array(
                'st_code' =>$val['st_code'],
                'st_id' =>$val['st_id'],
            );
            $shipTypeList = self::getByCondition($condition);
            if(!empty($shipTypeList)){
                $error[] ='货运方式代号已经存在';
            }
        }
        return $error;

    }

    public static function getAllShipTypes(){
        $obj = new Default_Model_ShipType;
        return $obj->getByCondition(array());
    }

}